using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace IUPUI.StellarResults.Client.UIElements
{
	/// <summary>
	/// A view of <see cref="Reading"/>s from multiple <see cref="Observatory"/>.
	/// </summary>
	public partial class ReadingsView : UserControl, ISimulationViewTabControl
	{
		#region Private Properties
		private Simulation m_Simulation = null;
		private List<ReadingsControl> m_ReadingControls = 
			new List<ReadingsControl>();
		#endregion Private Properties

		#region Public Properties
		/// <summary>
		/// Gets or sets the simulation.
		/// </summary>
		/// <value>The simulation.</value>
		public Simulation Simulation
		{
			get
			{
				return m_Simulation;
			}
			set
			{
				if (m_Simulation != value)
				{
					m_Simulation = value;
					OnSimulationChanged();
				}
			}
		}
		#endregion Public Properties

		#region Constructors
		/// <summary>
		/// Initializes a new instance of the <see cref="ReadingsView"/> class.
		/// </summary>
		public ReadingsView()
		{
			InitializeComponent();

			OnSimulationChanged();

			WriteNonVisibleObjects(m_WriteNonVisibleToolStripButton.Checked);
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="ReadingsView"/> class.
		/// </summary>
		public ReadingsView(
			IContainer container) : this()
		{
			container.Add(this);
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="ReadingsView"/> class.
		/// </summary>
		public ReadingsView(
			IContainer container,
			Simulation simulation)
			: this(container)
		{
			Simulation = simulation;
		}
		#endregion Constructors

		#region Public Methods
		/// <summary>
		/// Clears this instance.
		/// </summary>
		public void Clear()
		{
			// Clear each control
			foreach (ReadingsControl readingsControl in m_ReadingControls)
			{
				readingsControl.Clear();
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public void WriteNonVisibleObjects(
			bool value)
		{
			// Clear each control
			foreach (ReadingsControl readingsControl in m_ReadingControls)
			{
				readingsControl.WriteNonVisibleObjects = value;
			}
		}
		#endregion Public Methods

		/// <summary>
		/// Called when [simulation changed].
		/// </summary>
		protected virtual void OnSimulationChanged()
		{
			m_TabControl.TabPages.Clear();
			m_ReadingControls.Clear();

			// Add tab page for each observatory
			if (m_Simulation != null)
			{
				foreach (Observatory observatory in m_Simulation.Observatories)
				{
					TabPage tabPage = new TabPage(observatory.Name);
					ReadingsControl readingsControl = 
						new ReadingsControl(this.components, observatory);
					readingsControl.Dock = DockStyle.Fill;
					tabPage.Controls.Add(readingsControl);
					m_TabControl.Controls.Add(tabPage);
					m_ReadingControls.Add(readingsControl);
				}
			}
		}

		/// <summary>
		/// Handles the Click event of the m_ClearToolStripButton control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		private void m_ClearToolStripButton_Click(object sender, EventArgs e)
		{
			Clear();
		}

		/// <summary>
		/// Handles the Click event of the m_WriteNonVisibleToolStripButton control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		private void m_WriteNonVisibleToolStripButton_Click(object sender, EventArgs e)
		{
			m_WriteNonVisibleToolStripButton.Checked =
				!m_WriteNonVisibleToolStripButton.Checked;
			WriteNonVisibleObjects(
				m_WriteNonVisibleToolStripButton.Checked);
		}
	}
}
